package gw.com.android.ui.home;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.gwtsz.gts2.hx.R;


/**
 * 环状饼图
 */
public class AnnulusPie extends View {
    public AnnulusPie(Context context) {
        this(context, null);
    }

    public AnnulusPie(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnnulusPie(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dealAttr(context, attrs);
        init();
    }

    private void dealAttr(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AnnulusPie);

        if (ta != null) {
            try {
                paintWidth = ta.getInteger(R.styleable.AnnulusPie_annulusWidth, 80);//环的厚度
                annulusColor1 = ta.getColor(R.styleable.AnnulusPie_annulusColor1, 0x00FE6665);
                annulusColor2 = ta.getColor(R.styleable.AnnulusPie_annulusColor2, 0x0053BB86);//顏色表示法，0x是16位数的标识，00 是不透明度（00表示完全不透明），后面的6位表示颜色的16进制数
                innerCircleColor = ta.getColor(R.styleable.AnnulusPie_innerCircleColor, 0x00cccccc);
                shadowColor1 = ta.getColor(R.styleable.AnnulusPie_annulusColor1, 0x00FE6665);
                shadowColor2 = ta.getColor(R.styleable.AnnulusPie_annulusColor2, 0x0053BB86);
            } catch (Exception e) {

            } finally {
                ta.recycle();
            }
        }
    }


    public void setShadowColor1(int color) {
        this.shadowColor1 = color;
        postInvalidate();
    }

    public void setShadowColor2(int color) {
        this.shadowColor2 = color;
        postInvalidate();
    }

    public void setAnnulusColor1(int annulusColor1) {
        this.annulusColor1 = annulusColor1;
        initPaint();
        postInvalidate();
    }

    public void setAnnulusColor2(int annulusColor2) {
        this.annulusColor2 = annulusColor2;
        initPaint2();
        postInvalidate();
    }

    private int annulusColor1 = 0x00FE6665, annulusColor2 = 0x0053BB86, innerCircleColor = 0x00cccccc, shadowColor1 = 0x00FE6665, shadowColor2 = 0x0053BB86;
    private Paint paint, paint2, paint3, paint4, paint5;
    private int minWidth = 300, minHeight = 300;

    /**
     * 重写onMeasure设定最小宽高
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(minWidth, minHeight);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(minWidth, heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSize, minHeight);
        }

    }

    private float rotateValue = 90;
    private float MAX_RANGE = 354;//最大弧度数，因为有一部分会用来作为间隔, 所以并不是360D
    private float rangeSeparator = 4;// 缺口角度

    public void setValues(float value1, float value2) {
        if (value1 == 0 || value2 == 0) {
            rangeSeparator = 0;
            MAX_RANGE = 360;
        } else {
            rangeSeparator = 3;
            MAX_RANGE = 354;
        }
        setValue1(value1);
        setValue2(value2);
    }

    /**
     * 接受百分比的小数值
     *
     * @param value1
     */
    public void setValue1(float value1) {
        this.value1 = MAX_RANGE * value1;
    }

    public void setValue2(float value2) {
        this.value2 = MAX_RANGE * value2;
    }

    private float value1 = 240, value2 = 120;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int padding = lightWidth + paintWidth;
        padding = paintWidth;
        RectF oval = new RectF(0 + padding, 0 + padding, getWidth() - padding, getHeight() - padding);

        //这个圆弧的半径是多少?取宽高中的较小值，
        float radio = Math.min(oval.width(), oval.height()) / 2 + 2;

        canvas.drawArc(oval, 0 - rotateValue, value1, false, paint);//圆环1部分
        canvas.drawArc(oval, value1 - rotateValue, rangeSeparator, false, paint3);//空白分隔区域
        canvas.drawArc(oval, value1 + rangeSeparator - rotateValue, value2, false, paint2);//圆环2部分
        canvas.drawArc(oval, value1 + rangeSeparator + value2 - rotateValue, rangeSeparator, false, paint3);//空白分割区域2

        //然后在里面画一个圆，随底色
        // 圆心位置
//        float centerX = getWidth() / 2;
//        float centerY = getHeight() / 2;
//        canvas.drawCircle(centerX, centerY, radio - paintWidth / 2, paint4);

//        canvas.translate(centerX, centerY);
//        canvas.drawLine(0, 0, 0, -radio - paintWidth / 2 - lightWidth, paint5);
//        canvas.rotate(value1);
//        canvas.drawLine(0, 0, 0, -radio - paintWidth / 2 - lightWidth, paint5);
    }

    int paintWidth;//画笔宽度；单位px
    int paintWidthDp = 8;
    int lightWidth;// 发光宽度
    int lightWidthDp = 2;
    int shadowYDp = 1;


    /**
     * setShadowLayer(float radius, float dx, float dy, int shadowColor)
     * 在这里备注一下该方法的几个参数，
     * radius : 阴影的大小，数值越大，阴影范围越大
     * dx ,dy : 阴影相对于原控件的偏移量，如果我设置x=0，y=1，就是说，让阴影在原控件的下方1个px处;
     * shadowColor: 阴影颜色，注意，这里的颜色必须要用包含透明度的值，才能让阴影有发散模糊效果。如果你不写透明度。。。他看上去就像是把原控件复制了一次，然后放在原控件的下层，xy都不为0的话就挪动一下...显然不是我们要的阴影效果
     */
    private void initPaint() {
        paint = new Paint();
        paint.setColor(annulusColor1);
        paint.setShadowLayer(lightWidth, 0F, dp2px(shadowYDp), shadowColor1);
        paint.setAntiAlias(true);//抗锯齿
        paint.setStrokeWidth(paintWidth);
        paint.setStyle(Paint.Style.STROKE);//FILL填充,stroke描边
//        paint.setMaskFilter(new BlurMaskFilter(lightWidth, BlurMaskFilter.Blur.SOLID));//SOLID外发光,这种方式貌似不可以控制方向
    }


    private void initPaint2() {
        paint2 = new Paint();
        paint2.setColor(annulusColor2);
        paint2.setShadowLayer(lightWidth, 0F, dp2px(shadowYDp), shadowColor2);
        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(paintWidth);
        paint2.setStyle(Paint.Style.STROKE);//FILL填充,stroke描边
//        paint2.setMaskFilter(new BlurMaskFilter(lightWidth, BlurMaskFilter.Blur.SOLID));//SOLID外发光
    }

    private void initPaint3() {
        paint3 = new Paint();
        paint3.setColor(Color.TRANSPARENT);
        paint3.setAntiAlias(true);
        paint3.setStrokeWidth(paintWidth);
        paint3.setStyle(Paint.Style.STROKE);//FILL填充,stroke描边
    }

    private void initPaint4() {
        paint4 = new Paint();
        paint4.setColor(innerCircleColor);
        paint4.setAntiAlias(true);
        paint4.setStyle(Paint.Style.FILL);//FILL填充,stroke描边
    }

    private void initPaint5() {
        paint5 = new Paint();
        paint5.setColor(innerCircleColor);
        paint5.setAntiAlias(true);
        paint5.setStrokeWidth(dp2px(2));
        paint5.setStyle(Paint.Style.FILL);//FILL填充,stroke描边
    }

    private void init() {
        paintWidth = dp2px(paintWidthDp);
        lightWidth = dp2px(lightWidthDp);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        initPaint();
        initPaint2();
        initPaint3();
        initPaint4();
        initPaint5();
    }

    private int dp2px(float dp) {
        return (int) (dp * this.getResources().getDisplayMetrics().density + 0.5f);
    }

    private int transColor() {
        return 0;
    }
}